/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.io.xml;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.apache.commons.lang.StringUtils;

import phoside.util.StaticFinalValues;

/**
 *
 * @author gjj
 */
public class PhosphoProteinFieldValueFormatter
        extends DefaultProteinFieldValueFormatter {
    @Override
    public String write(String fieldName, Object fieldValue) {
        if (fieldValue==null)
            return null;

        if (fieldName.compareTo(StaticFinalValues.SITES)==0) {
            TreeSet<Integer> set = new TreeSet();
            for (int site : (Set<Integer>)fieldValue) {
                set.add(site+1);
            }

            if (set.isEmpty())
                return null;

            return super.write(fieldName, set);
        }

        if (fieldName.compareTo(StaticFinalValues.SITENOTES)==0) {
            TreeMap<Integer,String> map = new TreeMap();
            for (Map.Entry<Integer,List<String>> entry : ((Map<Integer,List<String>>)fieldValue).entrySet()) {
                map.put(entry.getKey()+1, StringUtils.join(entry.getValue(),'|'));
            }

            return super.write(fieldName, map);
        }

        return super.write(fieldName, fieldValue);
    }

    @Override
    public Object read(String fieldName, String value) {
        if (fieldName==null || value==null) {
            return null;
        }

        if (fieldName.compareTo(StaticFinalValues.SITES)==0) {
            TreeSet<Integer> sites = new TreeSet();

            if (value.length()==0)
                return sites;

            String[] strs = value.split(StaticFinalValues.SEPARATOR);
            for (String str : strs) {
                sites.add(Integer.valueOf(str)-1);
            }

            return sites;
        }

        if (fieldName.compareTo(StaticFinalValues.DISORDER)==0) {
            String[] strs = value.split(StaticFinalValues.SEPARATOR);
            ArrayList<Double> scores = new ArrayList(strs.length);
            for (String str : strs) {
                scores.add(Double.valueOf(str));
            }

            return scores;
        }

        if (fieldName.compareTo(StaticFinalValues.SITENOTES)==0) {
            String[] strs = value.split(StaticFinalValues.SEPARATOR);
            Map<Integer,List<String>> mapSiteNotes = new TreeMap();
            for (String str : strs) {
                String[] sn = str.split(":",2);
                Integer site = Integer.valueOf(sn[0])-1;
                List<String> notes = new ArrayList();
                if (sn.length>0) {
                    String[] ns = sn[1].split("\\|");
                    for (String n:ns)
                        notes.add(n);
                }
                if (!notes.isEmpty()) {
                    mapSiteNotes.put(site, notes);
                }
            }

            return mapSiteNotes;
        }

        return super.read(fieldName, value);
    }
}
